package com.class5.springboot.service.real;


import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.TbXtMenu;
import com.class5.springboot.entity.TbXtRole;
import com.class5.springboot.entity.TbXtRoleMenu;
import com.class5.springboot.entity.vo.SysMenuListVo;
import com.class5.springboot.entity.vo.TbXtRoleVo;
import com.class5.springboot.frame.context.SuperServiceImpl;
import com.class5.springboot.frame.repo.Sql;
import com.class5.springboot.service.facade.IRoleService;
import com.class5.springboot.util.BeanCopyUtils;
import com.class5.springboot.util.DateUtils;
import com.class5.springboot.util.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService extends SuperServiceImpl implements IRoleService {

    @Override
    @Transactional(readOnly = true)
    public List findRoleList(JSONObject param) {
        Sql sql = Sql.getSelect(TbXtRole.class);
        List<TbXtRole> roleList = repo.query(sql,TbXtRole.class);
        List<TbXtRoleVo> list = new ArrayList<>();
        for(TbXtRole xtRole:roleList){
            TbXtRoleVo vo = new TbXtRoleVo();
            BeanCopyUtils.copyNoEqClass(xtRole,vo);
            vo.setCreateTime(DateUtils.TimestampToDateString(xtRole.getCreateTime(),"1"));
            vo.setId(xtRole.getId()+"");
            list.add(vo);
        }
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public List getMenulistByRoleId(Long roleId) {
        List<TbXtRoleMenu> menuList = repo.query(Sql.getSelect(TbXtRoleMenu.class).where().eq("role_id",roleId).parent(),TbXtRoleMenu.class);
        List<SysMenuListVo> list = new ArrayList<>();
        for(TbXtRoleMenu tbXtRoleMenu:menuList){
            SysMenuListVo vo = new SysMenuListVo();
            if(tbXtRoleMenu.getMenuLevel().equals("1")){
                vo.setId(tbXtRoleMenu.getId());
                vo.setMenuUrl(tbXtRoleMenu.getMenuUrl());
                vo.setMenuName(tbXtRoleMenu.getMenuName());
                vo.setMenuLevel(tbXtRoleMenu.getMenuLevel());
                list.add(vo);
            }
        }
        for(SysMenuListVo vo:list){
            List<SysMenuListVo> childList = new ArrayList<>();
            for(TbXtRoleMenu tbXtRoleMenu:menuList){
                SysMenuListVo cvo = new SysMenuListVo();
                if(!tbXtRoleMenu.getMenuLevel().equals("1")){
                    cvo.setId(tbXtRoleMenu.getId());
                    cvo.setMenuUrl(tbXtRoleMenu.getMenuUrl());
                    cvo.setMenuName(tbXtRoleMenu.getMenuName());
                    cvo.setMenuLevel(tbXtRoleMenu.getMenuLevel());
                    if(tbXtRoleMenu.getParentId()==vo.getId()){
                        childList.add(cvo);
                    }
                }
            }
            vo.setChildMenuList(childList);
        }

        return list;
    }

    @Override
    public void createRole(JSONObject param) {
        String rolename = param.getString("roleName");
        TbXtRole role = new TbXtRole();
        role.setRoleName(rolename);
        repo.insert(role);
    }

    @Override
    @Transactional(readOnly = true)
    public TbXtRole findRoleById(Long id) {
        TbXtRole role = repo.get(id,TbXtRole.class);
        if(role==null){
            throw new ServiceException("查询不到该角色");
        }
        return role;
    }

    @Override
    public void updateRole(JSONObject param) {
        TbXtRole role = repo.get(param.getLong("id"),TbXtRole.class);
        if(role==null){
            throw new ServiceException("查询不到该角色");
        }
        role.setRoleName(param.getString("roleName"));
        repo.update(role);
    }

    @Override
    public void deleteRoleById(Long id) {
        TbXtRole role = repo.get(id,TbXtRole.class);
        if(role==null){
            throw new ServiceException("查询不到该角色");
        }
        repo.remove(role);
    }
}
